//
//  JQTextField.m
//  JQTextFieldDemo
//
//  Created by James on 15/7/21.
//  Copyright (c) 2015年 JQ. All rights reserved.
//

#import "JQTextField.h"

#define kJQTextFieldCornerRadius 5

@implementation JQTextField

#pragma mark - Setter &getter

@synthesize borderColor = _borderColor;
@synthesize activeBorderColor = _activeBorderColor;

- (UIColor *)borderColor{
    if (_borderColor == nil) {
        _borderColor = [UIColor lightGrayColor];
    }
    return _borderColor;
}

- (void)setBorderColor:(UIColor *)borderColor{
    self.layer.borderColor = borderColor.CGColor;
    _borderColor = borderColor;
}

- (UIColor *)activeBorderColor{
    if (_activeBorderColor == nil) {
        _activeBorderColor = [UIColor darkGrayColor];
    }
    return _activeBorderColor;
}

- (void)setActiveBorderColor:(UIColor *)activeBorderColor{
    _activeBorderColor = activeBorderColor;
    if ([self isFirstResponder]) {
        self.layer.borderColor = activeBorderColor.CGColor;
    }
}

#pragma mark - Init

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

#pragma mark - Draw
- (void)drawRect:(CGRect)rect{
    self.layer.cornerRadius = kJQTextFieldCornerRadius;
    self.layer.borderColor = self.borderColor.CGColor;
    self.layer.borderWidth = 1.f;
    self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.layer.masksToBounds = NO;
}

- (CGRect)borderRectForBounds:(CGRect)bounds{
    return CGRectMake(10, 0, bounds.size.width - 10, bounds.size.height);
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds{
    return CGRectMake(10, 0, bounds.size.width - 10, bounds.size.height);
}

- (CGRect)textRectForBounds:(CGRect)bounds{
    return CGRectMake(10, 0, bounds.size.width - 10, bounds.size.height);
}

- (CGRect)editingRectForBounds:(CGRect)bounds{
    return CGRectMake(10, 0, bounds.size.width - 10, bounds.size.height);
}

#pragma mark - Active

- (BOOL)becomeFirstResponder{
    self.layer.borderColor = self.activeBorderColor.CGColor;
    return [super becomeFirstResponder];
}

- (BOOL)resignFirstResponder{
    self.layer.borderColor = self.borderColor.CGColor;
    return [super resignFirstResponder];
}

#pragma mark - Wrong animate

- (void)wrongInputWithAnimate:(JQWrongInputAnimate)animateType{
    if (animateType == JQWrongInputAnimateShake) {
        self.layer.transform = CATransform3DMakeTranslation(10.0, 0.0, 0.0);
        [UIView animateWithDuration:0.8 delay:0.0 usingSpringWithDamping:0.2 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.layer.transform = CATransform3DIdentity;
        } completion:^(BOOL finished) {
            self.layer.transform = CATransform3DIdentity;
        }];
    }
    else if (animateType == JQWrongInputAnimateGlowEdge){
        self.layer.shadowColor = [UIColor redColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(0, 0);
        self.layer.shadowRadius = 3;
        self.layer.shadowOpacity = 0;
        self.layer.borderColor = [UIColor redColor].CGColor;
        [UIView animateWithDuration:1.5 animations:^{
            self.layer.shadowOpacity = 1;
        }];
    }
}

@end
